import axios from "axios";
import save from "utils/save";
import router from "@/router";
import ElementUi from "element-ui";

const request = axios.create({
  // baseURL: "/api",
  timeout: 20000,
});

const debounse = function (fnc, delay) {
  let timer = null;
  return function () {
    clearInterval(timer);
    timer = setTimeout(() => fnc(), delay);
  };
};
request.interceptors.request.use(function (config) {
  const accessPaths = ["/login", "/getCaptcha", "/verifyCaptcha"];
  const veri = accessPaths.some((item) => new RegExp(item).test(config.url));
  if (!veri) {
    config.headers["authorization"] = save.takeOut("TOKKU");
  }
  return config;
});
const lastMsg = debounse(function () {
  ElementUi.Message.error("登录过期，重新登录");
  router.push({ name: "login" });
  save.remove("TOKKU");
}, 200);
request.interceptors.response.use(function (config) {
  if (config.data.code === "1004" || config.data.code === "10022") {
    lastMsg();

  }
  return config;
});

export default request;
